package com.dbh.company.map.baidu;

import cn.hutool.core.text.PasswdStrength;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author dongbanghui
 * @create 2023/8/11
 */
public class RoutePlanning {

    public static void main(String[] args) throws UnsupportedEncodingException {
        for (int i = 0; i < 13; i++) {
            String result = search(i + "");
            JSONObject jsonObject1 = JSONUtil.parseObj(result);
            String jsonStr = JSONUtil.toJsonStr(jsonObject1.get("results"));
            JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
            for (Object o : jsonArray) {
                String jsonStr1 = JSONUtil.toJsonStr(o);
                JSONObject entries = JSONUtil.parseObj(jsonStr1);
                Object name = entries.get("name");
                Object uid = entries.get("uid");
                System.out.println(name + "  " + uid);
            }
        }

//        System.out.println(getIsochrone());


//        System.out.println(directionlite());

//        System.out.println(regionSearch());

//        System.out.println(search());
    }

    /**
     * 道路路况查询  https://lbs.baidu.com/faq/api?title=webapi/traffic-roadseek
     * @return
     * @throws UnsupportedEncodingException
     */
    private static String traffic() throws UnsupportedEncodingException {
        RoutePlanning snCal = new RoutePlanning();
        String timestamp = String.valueOf(new Date().getTime());
        String ak = "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o";
        Map paramsMap = new LinkedHashMap<String, String>();
        String road_name = "北清路";
        String city = "北京";
        paramsMap.put("road_name", road_name);
        paramsMap.put("city", city);
        paramsMap.put("ak", ak);
        paramsMap.put("timestamp", timestamp);
        String paramsStr = snCal.toQueryString(paramsMap);
        String wholeStr = new String("/traffic/v1/road?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");
        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
        String paramTemp = "road_name=" + URLEncoder.encode(road_name) + "&city=" + URLEncoder.encode(city) + "&ak=" + ak +
                "&timestamp=" + timestamp
                + "&sn=" + snCal.MD5(tempStr);
        String url = "https://api.map.baidu.com/traffic/v1/road?" + paramTemp;
        String s = HttpUtil.get(url);
        return s;
    }

    /**
     * 多边形内搜索
     * @param page_num 当前页码
     * @return 结果
     * @throws UnsupportedEncodingException 异常
     */
    private static String search(String page_num) throws UnsupportedEncodingException {
        RoutePlanning snCal = new RoutePlanning();
        String timestamp = String.valueOf(new Date().getTime());
        Map paramsMap = new LinkedHashMap<String, String>();
        String query = "小区$住宅区";
        String bounds = "116.46819290951,39.93532948004;116.47032189357,39.929457941245;116.47010630025,39.928814738298;116.46800426535,39.927521397805;116.4630725681,39.927957124893;116.46215629648,39.928482760201;116.46171612678,39.928911564938;116.46172510984,39.929291953971;116.46477934857,39.934084673235;116.46556985742,39.934962955962";
        String[] latNats = bounds.split(";");
        String boundsStr = "";
        String originPoi = "";
        for (int i = 0; i < latNats.length; i++) {
            String str = latNats[i];
            String[] split = str.split(",");
            String tempStr = split[1] + "," + split[0];
            if (i == 0) {
                originPoi = tempStr;
            }
            boundsStr += tempStr + ",";
        }
        boundsStr = boundsStr.substring(0, boundsStr.length() - 1) + "," + originPoi;
        String ak = "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o";
        String page_size = "10";
//        String tag = "房地产";
        paramsMap.put("query", query);
//        paramsMap.put("tag", tag);
        paramsMap.put("bounds", boundsStr);
        paramsMap.put("output", "json");
        paramsMap.put("page_num", page_num);
        paramsMap.put("page_size", page_size);
        paramsMap.put("ak", ak);
        paramsMap.put("timestamp", timestamp);
        String paramsStr = snCal.toQueryString(paramsMap);
        String wholeStr = new String("/place/v2/search?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");
        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
        String paramTemp = "query=" + URLEncoder.encode(query) /*+ "&tag=" + tag*/
                + "&bounds=" + URLEncoder.encode(boundsStr) +
                "&output=json" + "&page_num=" + page_num + "&page_size=10" + "&ak=" + ak +
                "&timestamp=" + timestamp
                + "&sn=" + snCal.MD5(tempStr);
        String url = "https://api.map.baidu.com/place/v2/search?" + paramTemp;
        String s = HttpUtil.get(url);
        return s;
    }

    /**
     * 等时圈
     * @return 结果
     * @throws UnsupportedEncodingException 异常
     */
    private static String getIsochrone() throws UnsupportedEncodingException {
        RoutePlanning snCal = new RoutePlanning();
        String timestamp = String.valueOf(new Date().getTime());
        Map paramsMap = new LinkedHashMap<String, String>();
        String mode = "riding";
        String location = "39.931336,116.465487";
        String radius = "600";
        String coord_type = "bd09ll";
        String result_type = "3";
        String scope = "2";
        paramsMap.put("mode", mode);
        paramsMap.put("location", location);
        paramsMap.put("radius", radius);
        paramsMap.put("coord_type", coord_type);
        paramsMap.put("result_type", result_type);
        paramsMap.put("scope", scope);
        paramsMap.put("ak", "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o");
        paramsMap.put("timestamp", timestamp);

        String paramsStr = snCal.toQueryString(paramsMap);

        String wholeStr = new String("/reachregion/v1/equaldistance?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");

        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");

        String paramTemp = "mode=" + URLEncoder.encode(mode) + "&location=" + URLEncoder.encode(location) + "&radius=" + radius + "&coord_type=" + coord_type
                + "&result_type=" + result_type  + "&scope=" + scope + "&ak=lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o" +
                "&timestamp=" + timestamp
                + "&sn=" + snCal.MD5(tempStr);
        String url = "https://api.map.baidu.com/reachregion/v1/equaldistance?" + paramTemp;
        String s = HttpUtil.get(url);
        return s;
    }


    public static String directionlite() throws UnsupportedEncodingException {

        RoutePlanning snCal = new RoutePlanning();
        String timestamp = String.valueOf(new Date().getTime());
        Map paramsMap = new LinkedHashMap<String, String>();
        String origin = "39.931336,116.465487";
        String destination = "39.965978,116.507078";
        String ak = "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o";
        paramsMap.put("origin", origin);
        paramsMap.put("destination", destination);
        paramsMap.put("ak", ak);
        paramsMap.put("timestamp", timestamp);
        String paramsStr = snCal.toQueryString(paramsMap);
        String wholeStr = new String("/directionlite/v1/driving?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");
        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
        String paramTemp = "origin=" + URLEncoder.encode(origin) + "&destination=" + URLEncoder.encode(destination)
                + "&ak=" + ak +
                "&timestamp=" + timestamp
                + "&sn=" + snCal.MD5(tempStr);
        String url = "https://api.map.baidu.com/directionlite/v1/driving?" + paramTemp;
        String s = HttpUtil.get(url);
        return s;
    }

    public static String regionSearch() throws UnsupportedEncodingException {
        RoutePlanning snCal = new RoutePlanning();
        String timestamp = String.valueOf(new Date().getTime());
        Map paramsMap = new LinkedHashMap<String, String>();
        String keyword = "房山区";
        String sub_admin = "3";
        String boundary = "1";
        String ak = "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o";
        paramsMap.put("keyword", keyword);
        paramsMap.put("sub_admin", sub_admin);
        paramsMap.put("boundary", boundary);
        paramsMap.put("ak", ak);
        paramsMap.put("timestamp", timestamp);
        String paramsStr = snCal.toQueryString(paramsMap);
        String wholeStr = new String("/api_region_search/v1/?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");
        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
        String paramTemp = "keyword=" + URLEncoder.encode(keyword) + "&sub_admin=" + sub_admin
                + "&boundary=" + boundary
                + "&ak=" + ak +
                "&timestamp=" + timestamp
                + "&sn=" + snCal.MD5(tempStr);
        String url = "https://api.map.baidu.com/api_region_search/v1/?" + paramTemp;
        String s = HttpUtil.get(url);
        return s;
    }

//    public static String polygon() throws UnsupportedEncodingException {
//        RoutePlanning snCal = new RoutePlanning();
//        String timestamp = String.valueOf(new Date().getTime());
//        Map paramsMap = new LinkedHashMap<String, String>();
//        String ak = "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o";
//        String vertexes = "116.46804918063,39.952546966015;116.48390427288,39.939568567569;116.4907224117,39.92952710249;116.49115359834,39.928876984011;116.49120749667,39.928683330495;116.49123444584,39.926885092907;116.48711122355,39.921358672493;116.48443427313,39.91945648386;116.47651121853,39.914641979368;116.46821985867,39.912746520501;116.45692815841,39.915188470462;116.44622933979,39.924014730414;116.44145933753,39.929623928114;116.44124374421,39.930675168858;116.44136052393,39.931484336505;116.46423138221,39.950707876675";
//        String[] latNats = vertexes.split(";");
//        paramsMap.put("vertexes", vertexes);
//        paramsMap.put("ak", ak);
//        paramsMap.put("timestamp", timestamp);
//        String paramsStr = snCal.toQueryString(paramsMap);
//        String wholeStr = new String("/reverse_geocoding/v3/?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");
//        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
//        String paramTemp = "ak=" + ak + "&output=" + output
//                + "&location=" + URLEncoder.encode(location)
//                + "&radius=" + radius +
//                "&timestamp=" + timestamp
//                + "&sn=" + snCal.MD5(tempStr);
//        String url = "https://api.map.baidu.com/traffic/v1/polygon?" + paramTemp;
//        String s = HttpUtil.get(url);
//        return s;
//    }

    /**
     *  https://lbsyun.baidu.com/faq/api?title=webapi/guide/webservice-placeapi/district
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String search() throws UnsupportedEncodingException {
        RoutePlanning snCal = new RoutePlanning();
        String timestamp = String.valueOf(new Date().getTime());
        Map paramsMap = new LinkedHashMap<String, String>();
        String query = "小区";
        String region = "131";
        String output= "json";
        String city_limit = "true";
        String ak = "lknUbGcMfhRfdVWgAKh9xumrRrSeqQ5o";
        paramsMap.put("query", query);
        paramsMap.put("region", region);
        paramsMap.put("city_limit", city_limit);
        paramsMap.put("output", output);
        paramsMap.put("ak", ak);
        paramsMap.put("timestamp", timestamp);
        String paramsStr = snCal.toQueryString(paramsMap);
        String wholeStr = new String("/api_region_search/v1/?" + paramsStr + "3XQHe7mOziKNjK2GweliKL8pgHbF0xsm");
        String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
        String paramTemp = "query=" + URLEncoder.encode(query) +  "&region=" + URLEncoder.encode(region)
                + "&city_limit=" + city_limit
                + "&output=" + output
                + "&ak=" + ak +
                "&timestamp=" + timestamp
                + "&sn=" + snCal.MD5(tempStr);
        String url = "https://api.map.baidu.com/api_region_search/v1/?" + paramTemp;
        String s = HttpUtil.get(url);
        return s;

    }




    // 对Map内所有value作utf8编码，拼接返回结果
    public String toQueryString(Map<?, ?> data)
            throws UnsupportedEncodingException {
        StringBuffer queryString = new StringBuffer();
        for (Map.Entry<?, ?> pair : data.entrySet()) {
            queryString.append(pair.getKey() + "=");
            queryString.append(URLEncoder.encode((String) pair.getValue(),
                    "UTF-8") + "&");
        }
        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }
        return queryString.toString();
    }

    // 来自stackoverflow的MD5计算方法，调用了MessageDigest库函数，并把byte数组结果转换成16进制
    public String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                        .substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }

        return null;
    }


}
